<template>
  <div v-if="visible">
    <el-dialog :visible.sync="visible"
               :title="!dataForm.id ? $t('add') : $t('update')"
               :modal="false"
               class="user-dialog add-or-updata"
               width="40%"
               :before-close="closeDialog"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form :model="dataForm"
               v-loading="dataListLoading"
               :element-loading-text="$t('loading')"
               ref="dataForm"
               label-position="left"
               @keyup.enter.native="dataFormSubmitHandle()"
               label-width="80px">
        <el-form-item prop="name"
                      :rules="required()"
                      label="品牌">
          <el-input v-model.trim="dataForm.name"
                    maxlength="20"
                    show-word-limit
                    placeholder="请输入品牌名称"></el-input>
        </el-form-item>
        <el-form-item prop="remarks"
                      label="备注">
          <el-input type="textarea"
                    :autosize="{ minRows: 3, maxRows: 4}"
                    maxlength="100"
                    show-word-limit
                    v-model="dataForm.remarks"
                    placeholder="备注"></el-input>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button @click="closeDialog()">{{ $t("cancel") }}</el-button>
        <el-button type="primary"
                   :loading="dataListLoading"
                   @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import minFormValidate from '@/mixins/form-validate'
import debounce from 'lodash/debounce'
export default {
  mixins: [mixinViewModule, minFormValidate],
  data () {
    return {
      mixinViewModuleOptions: {
        deleteImgURL: '/system/oss/delete',
        getDataListURL: '', // 数据列表接口，API地址
        getDataListIsPage: true, // 数据列表接口，是否需要分页？
        addURL: 'tb/deviceBrand', // 添加或修改接口，API地址
        updateURL: 'tb/deviceBrand/', // 添加或修改接口，API地址
        getDataInfoURL: '', // 数据详情，API地址
        deleteURL: '', // 删除接口，API地址
        deleteIsBatch: true, // 删除接口，是否需要批量？
        exportURL: '' // 导出接口，API地址
      },
      tempDeleteFileList: [], // 临时删除的文件
      newEditFileList: [], // 保存在编辑时新增的文件路径
      newThumbnailList: [], // 保存在编辑时新增的thumbnail图路径
      tempThumbnailList: [], // 临时删除的thumbnail图的图片
      visible: false,
      dataForm: {
        remarks: '',
        name: ''
      }
    }
  },
  computed: {
  },
  components: {},
  methods: {
    init (data) {
      if (data) {
        this.dataForm = data
      }
      this.visible = true
      this.$nextTick(() => {
      })
    },
    closeDialog () {
      this.dataForm.id = ''
      this.dataForm.remarks = ''
      this.dataForm.name = ''
      this.$emit('callBack')
      this.visible = false
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            const url = this.dataForm.id ? this.mixinViewModuleOptions.updateURL
              : this.mixinViewModuleOptions.addURL
            if (!this.dataForm.id) {
              this.$http.post(url, this.dataForm).then(({ data: res }) => {
                if (res.code !== 0) {
                  return this.$message.error(res.msg)
                }
                this.$message.success('新增成功')
                this.$emit('callBack')
                this.closeDialog()
              })
            } else {
              this.$http.put(url, this.dataForm).then(({ data: res }) => {
                if (res.code !== 0) {
                  return this.$message.error(res.msg)
                }
                this.$message.success('保存成功')
                this.$emit('callBack')
                this.closeDialog()
              })
            }
          }
        })
      },
      1000,
      { leading: true, trailing: false }
    )
  }
}
</script>

<style lang="scss" scoped>
</style>
